package com.cmic.rcs.emoji.controller.app;


import com.cmic.rcs.emoji.common.UserContext;
import com.cmic.rcs.emoji.entity.pojo.TBusinessTopology;
import com.cmic.rcs.emoji.entity.pojo.TEnterpriseMember;
import com.cmic.rcs.emoji.entity.vo.TopologyResponseVO;
import com.cmic.rcs.emoji.mapper.TEnterpriseMemberMapper;
import com.cmic.rcs.emoji.service.BusinessTopologyService;
import com.cmic.rcs.enhance.message.g5.core.common.Response;
import com.cmic.rcs.enhance.message.g5.core.common.ResponseData;
import com.cmic.rcs.enhance.message.g5.core.common.ReturnCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 /**
  * 业务拓扑图
  * @author 孙高明
  * @date 2025-10-17
  */
@Slf4j
@RestController("APPbusinessTopologyController")
@RequestMapping("/api/app/businessTopology")
public class businessTopologyController {

    @Autowired
    private TEnterpriseMemberMapper enterpriseMemberMapper;

    @Autowired
    private BusinessTopologyService businessTopologyService;

    @RequestMapping("/getBusinessTopology")
     public Response getBusinessTopology() {
        try {
            log.info("获取业务拓扑图");
            //1.获取当前用户所在企业编码
            String phone = UserContext.getUserDto().getUsername();
            TEnterpriseMember menber = enterpriseMemberMapper.selectByEncryptedPhone(phone);
            if (menber == null) {
                log.warn("用户不存在: {}", phone);
                return Response.Error(ReturnCodeEnum.NULL_EXCEPTION, "用户不存在");
            }
            String enterpriseCode = menber.getEnterpriseCode();
            // 2. 根据企业编码查询拓扑图信息
            TBusinessTopology topology = businessTopologyService.getTopologyByEnterpriseCode(enterpriseCode);
            if (topology == null) {
                log.info("该企业暂无拓扑图配置: {}", enterpriseCode);
                return ResponseData.OK(null); // 返回null表示暂无拓扑图
            }
            // 3. 构建响应对象
            TopologyResponseVO responseVO = new TopologyResponseVO();
            responseVO.setImageUrl(topology.getImageUrl()); // 图片URL
            responseVO.setImageName(topology.getImageName()); // 图片名称
            responseVO.setImageType(topology.getImageType()); // 图片类型
            responseVO.setUpdateTime(topology.getUpdateTime().toString()); // 更新时间

            log.info("获取拓扑图成功，企业编码: {}, 图片类型: {}", enterpriseCode, topology.getImageType());
            return ResponseData.OK(responseVO);
        } catch (Exception e) {
            log.error("获取企业业务拓扑图异常: {}", e.getMessage(), e);
            return ResponseData.Error(ReturnCodeEnum.SYSTEM_ERROR, "获取拓扑图失败，请重试");
        }

    }
}
